package ftp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;


public class FTPManageUtilTest {
	
	private static FTPClient ftpClient = null;
	
	@BeforeClass
	public static void initServer () {
		FileOutputStream fos = null;
		try {
			File _file = new File("D:/ftp/新建文本文档.txt");
			fos = new FileOutputStream(_file);
			fos.write("一段测试内容".getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		ftpClient = FTPManageUtil.connectServer("admin", "admin", "127.0.0.1", 2121);
		FTPManageUtil.mkdirs(ftpClient, "test/move/t1/t1_1");
		FTPManageUtil.mkdirs(ftpClient, "test/rename/rename_before");
		FTPFile[] files = FTPManageUtil.listRemoteFiles(ftpClient, "/test");
		System.out.println("目录结构:");
		for (FTPFile file : files) {
			System.out.println(file.getName());
		}
	}
	
	@AfterClass
	public static void close () {
		FTPManageUtil.closeConnect(ftpClient);
	}
	
	@Test
	public void upAndDown(){
		String file = "D:/ftp/新建文本文档.txt";
		boolean upSucceed = FTPManageUtil.upFile(ftpClient, "/新建文本文档-.txt", file);
		System.out.println(upSucceed ? "上传成功" : "上传失败");
		
		boolean downSucceed = FTPManageUtil.downFile(ftpClient, "/新建文本文档-.txt", "D:/ftp/-222新建文本文档-.txt");
		System.out.println(downSucceed ? "下载成功" : "下载失败");
		
		FTPFile[] files = FTPManageUtil.listRemoteFiles(ftpClient, "/");
		for (FTPFile ftpFile : files) {
			System.out.println(ftpFile.getName());
		}
	}
	
	@Test
	public void testMkdirs(){
		// 在当前路径创建文件夹
		String folderName = "test/1";
		boolean isSuccess = FTPManageUtil.mkdirs(ftpClient, folderName);
		Assert.assertTrue("在当前路径创建文件夹失败", isSuccess);

		// 递归创建文件夹,当前目录开始
		folderName = "test/2/test_2";
		isSuccess = FTPManageUtil.mkdirs(ftpClient, folderName);
		Assert.assertTrue("递归创建文件夹失败-相对路径", isSuccess);

		// 递归创建文件夹,根目录开始
		folderName = "/test/2/test_3";
		isSuccess = FTPManageUtil.mkdirs(ftpClient, folderName);
		Assert.assertTrue("递归创建文件夹失败-绝对路径", isSuccess);
	}

	/**
	 * 重命名文件或文件夹名称
	 */
	@Test
	public void testRename(){
		String oldName = "test/rename/rename_before";
		String newName = "test/rename/rename_after";
		boolean isSuccess = FTPManageUtil.rename(ftpClient, oldName, newName);
		Assert.assertTrue("重命名失败", isSuccess);
	}

	/**
	 * 移动文件
	 */
	@Test
	public void testMove(){
		String oldPath = "test/move/t1";
		String newPath = "test/move/t2";
		boolean isSuccess = FTPManageUtil.move(ftpClient, oldPath, newPath);
		Assert.assertTrue("文件移动失败", isSuccess);
	}
}
